package com.system.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.business.domain.Train;
import com.system.business.domain.TrainCarriage;
import com.system.business.req.TrainCarriageQueryReq;
import com.system.business.req.TrainCarriageSaveReq;
import com.system.business.resp.TrainCarriageQueryResp;
import com.system.business.resp.TrainQueryResp;
import com.system.business.service.TrainCarriageService;
import com.system.business.mapper.TrainCarriageMapper;
import com.system.common.resp.PageResp;
import com.system.common.utils.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Admin
* @description 针对表【train_carriage(火车车厢)】的数据库操作Service实现
* @createDate 2024-03-21 15:02:16
*/
@Service
public class TrainCarriageServiceImpl extends ServiceImpl<TrainCarriageMapper, TrainCarriage>
    implements TrainCarriageService{


    @Autowired
    private TrainCarriageMapper trainCarriageMapper;

    @Override
    public PageResp<TrainCarriageQueryResp> queryList(TrainCarriageQueryReq req) {
        Page<TrainCarriage> pageCondition = new Page<>(req.getPage(), req.getSize());
        QueryWrapper<TrainCarriage> trainCarriageQueryWrapper = new QueryWrapper<>();
        Page<TrainCarriage> pageData = trainCarriageMapper.selectPage(pageCondition,trainCarriageQueryWrapper);
        List<TrainCarriageQueryResp> list = BeanUtil.copyToList(pageData.getRecords(), TrainCarriageQueryResp.class);

        PageResp<TrainCarriageQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageData.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public void save(TrainCarriageSaveReq req) {
        DateTime now = DateTime.now();

        TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);
        if (ObjectUtil.isNull(trainCarriage.getId())) {
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.insert(trainCarriage);
        } else {
            trainCarriage.setUpdateTime(now);
            trainCarriageMapper.updateById(trainCarriage);
        }
    }
}




